package com.ruoyi.service.impl;

import com.ruoyi.domain.RankingInfor;
import com.ruoyi.domain.SerivrceInfor;
import com.ruoyi.domain.vo.*;
import com.ruoyi.mapper.ServceUseingInforMapper;
import com.ruoyi.mapper.ServiceCreateInforMapper;
import com.ruoyi.mapper.ServiceSubInforMapper;
import com.ruoyi.service.ServiveStatisticsSerive;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Collectors;

@Service("ServiveStatisticsSeriveImpl")
public class ServiveStatisticsSeriveImpl implements ServiveStatisticsSerive {
    @Autowired
    ServiceCreateInforMapper serviceCreateInforMapper;
    @Autowired
    ServceUseingInforMapper servceUseingInforMapper;

    @Autowired
    ServiceSubInforMapper serviceSubInforMapper;
    @Override
    public CreateStatisticsInfor getCreateStatisticsInfor() {
        CreateStatisticsInfor createStatisticsInfor = new CreateStatisticsInfor();
        List<SerivrceInfor> serivrceInforList = serviceCreateInforMapper.getServiceCreateInfor();
        List<SerivrceInfor> serivrceInforListByType = serviceCreateInforMapper.getServiceCreateInforByType();

        List< CommonInfor> erList = new ArrayList<>();
        List< TypestatisticsInfor> typelist = new ArrayList<>();
         if(serivrceInforList.size()>0&&serivrceInforListByType.size()>0){
           int i=1;
        for(SerivrceInfor infor:serivrceInforList) {
            erList.add(new CommonInfor(infor.getTotal(), 0, i, infor.getTime()));
            i++;
        }
        int wtype=12;
        for(SerivrceInfor infor:serivrceInforListByType) {
            typelist.add(new TypestatisticsInfor(infor.getTotal(),  String.valueOf(wtype), "type"+infor.getType()));
            wtype++;
        }
       }else {
             return null;
         }
         createStatisticsInfor.setErlist(erList);
         createStatisticsInfor.setTypelist(typelist);
        return createStatisticsInfor;
    }

    @Override
    public SubStatisticsInfor getSubStatisticsInfor() {

        SubStatisticsInfor subStatisticsInfor = new SubStatisticsInfor();
        List<SerivrceInfor> serivrceInforList = serviceSubInforMapper.getServiceSubInfor();
        List<RankingInfor> recentRanking = serviceSubInforMapper.getSubRankingRecent();
        List<RankingInfor> ranking = serviceSubInforMapper.getSubRanking();

        List<CommonInfor> erList = new ArrayList<>();
        List<RankingInfor> recentRankingList = new ArrayList<>();
        List<RankingInfor> rankingList = new ArrayList<>();
        if(serivrceInforList.size()>0){
            recentRankingList=recentRanking;
            rankingList=ranking;
            AtomicInteger k= new AtomicInteger(1);
            recentRankingList= recentRankingList.stream().map(infor-> {
                k.set(0 - k.get());
                infor.setIsOnline(k.get());
               return infor;
            }).collect(Collectors.toList());

            rankingList= rankingList.stream().map(infor-> {
                k.set(0 - k.get());
                infor.setIsOnline(0-k.get());
                return infor;
            }).collect(Collectors.toList());
            int i=1;
            for(SerivrceInfor infor:serivrceInforList) {
                erList.add(new CommonInfor(infor.getTotal(), 0, i, infor.getTime()));
                i++;
            }
        }else {
            return null;
        }
        subStatisticsInfor.setErlist(erList);
        subStatisticsInfor.setRecentRanking(recentRankingList);
        subStatisticsInfor.setRanking(rankingList);
        return subStatisticsInfor;
    }

    @Override
    public UseingStatisticsInfor getUseingStatisticsInfor() {
        UseingStatisticsInfor useingStatisticsInfor = new UseingStatisticsInfor();
        List<SerivrceInfor> serivrceInforList = servceUseingInforMapper.getUseingInfor();
      List<RankingInfor> recentRanking = servceUseingInforMapper.getUseingRankingRecent();
      List<RankingInfor> ranking = servceUseingInforMapper.getUseingRanking();

        List<CommonInfor> erList = new ArrayList<>();
        List<RankingInfor> recentRankingList = new ArrayList<>();
        List<RankingInfor> rankingList = new ArrayList<>();
     if(serivrceInforList.size()>0){
        recentRankingList=recentRanking;
        rankingList=ranking;
         AtomicInteger k= new AtomicInteger(1);
         recentRankingList= recentRankingList.stream().map(infor-> {
             k.set(0 - k.get());
             infor.setIsOnline(k.get());
             return infor;
         }).collect(Collectors.toList());

         rankingList= rankingList.stream().map(infor-> {
             k.set(0 - k.get());
             infor.setIsOnline(0-k.get());
             return infor;
         }).collect(Collectors.toList());
         int i=1;
         for(SerivrceInfor infor:serivrceInforList) {
             erList.add(new CommonInfor(infor.getTotal(), 0, i, infor.getTime()));
         }
     }else {
         return null;
     }
     useingStatisticsInfor.setErlist(erList);
     useingStatisticsInfor.setRecentRanking(recentRankingList);
     useingStatisticsInfor.setRanking(rankingList);

        return useingStatisticsInfor;
    }


}
